--
-- Author: shunguo.chen
-- Date: 2017-11-27 11:42:45
--
-- MountModel

local Mount = require("app.public.dao.Mount")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ModelBase)

function ClassRef:ctor(data)
	self.super.ctor(self, data)
end

function ClassRef:init()
    self._haveMounts = nil
    self._noHaveMounts = nil
    self._speedData = nil

end

function ClassRef:clearDatas()
    self._haveMounts = nil
    self._noHaveMounts = nil

end

local function sortMount(a, b)
    if a.quality ~= b.quality then
        return a.quality > b.quality
    end

    if a.conf.type2icon ~= b.conf.type2icon then
        return b.conf.type2icon
    end

    if a.star ~= b.star then
        return a.star > b.star
    end

    return  a.tid < b.tid
end


function ClassRef:initMountData()
    local haveMounts = {}
    local noHaveMounts = {}

    local confs = GD:getMountConfigs()
    for k,v in pairs(confs) do
        local mount=UD:getMountByID(tonumber(v.ID))
        if mount then
            haveMounts[#haveMounts + 1] = mount
        elseif v.enable == "1" then
            mount = Mount.new()
            mount:read({uid=v.ID, tid=tonumber(v.ID)})
            noHaveMounts[#noHaveMounts + 1] = mount
        end
    end

    table.sort(haveMounts, sortMount)
    table.sort(noHaveMounts, sortMount)
    self._haveMounts = haveMounts
    self._noHaveMounts = noHaveMounts
end

function ClassRef:getHaveMounts()
    return self._haveMounts
end


-- 获取所有坐骑
function ClassRef:getMountDatas(isEquipAble, isSortedCanGet)
    if not self._haveMounts then
       self:initMountData()
    end
    
    local mounts = {}

    local noHaveMounts = self._noHaveMounts
    
    if isSortedCanGet then
        noHaveMounts = clone(self._noHaveMounts)
        table.sort(noHaveMounts, function(a, b)
            local aCanSum = a:checkComposition()
            local bCanSum = b:checkComposition()
            if aCanSum ~= bCanSum then
                if aCanSum then
                    return true
                else
                    return false
                end
            end
            return sortMount(a, b)
        end)
    end
    
    if isEquipAble then
        -- 筛选可以装备兽栏的坐骑
        for i,mount in ipairs(self._haveMounts) do
            if mount:canEquip() then
                mounts[#mounts + 1] = mount
            end
        end
        for i,mount in ipairs(noHaveMounts) do
            if mount:canEquip() then
                mounts[#mounts + 1] = mount
            end
        end
    else
        mounts = table.clone(self._haveMounts)
        mounts = table.attach(mounts, noHaveMounts)
    end
  

    return mounts
end

-- 获取 排除经腰带数目
function ClassRef:getMountNumWithoutSpReward(datas)
    if not datas then
        datas = self:getMountDatas()
    end
    local num = 0
    local haveNum = 0
    for k ,v in pairs(datas) do
        if not v:isSpRewardMount() then
            num = num + 1
            if UD:getMountByID(tonumber(v:getUid())) then
                haveNum = haveNum + 1
            end
        end
    end
    return num, haveNum
end

function ClassRef:updateMount(uid, mount, opt)
    if opt ~= "update" and self._speedData then
        -- 速度需要重新计算
        self._speedData = nil
    end
    
    if not self._haveMounts then return end
    if opt == "update" then
        table.sort(self._haveMounts, sortMount)
    elseif opt == "insert" then
        self._haveMounts[#self._haveMounts + 1] = mount
        table.sort(self._haveMounts, sortMount)
        if mount:isEnable() then
            for i,v in ipairs(self._noHaveMounts) do
                if v.tid == mount.tid then
                    table.remove(self._noHaveMounts, i)
                    break
                end
            end
        end
    elseif opt == "reload" then
        self:initMountData()
    end
end

-- 获取总星数
function ClassRef:getMountsTotalStar()
    local datas = self:getMountDatas()
    local starNum = 0 
    for k ,v in pairs(datas) do
        if not v:isSpRewardMount() then
            if UD:getMountByID(tonumber(v:getUid())) then
                local t = v:getStar() or 0
                starNum = starNum + t
            end 
        end
    end
    return starNum
end

-- 获取骑术相关数据
function ClassRef:getSpeedData()
    if not self._speedData then
        local _, hasNum = self:getMountNumWithoutSpReward()
        -- local hasNum = UD:getMountNum()
        local confs = GD:getMountSpeedConfs()
        local curLv = 0
        local curSpeed = 0
        local nextSpeed = 0
        local needNum = 0
        local lenth = #confs

        for i=1, lenth do
            local conf = confs[i]
            local lv = tonumber(conf.speedLevel)
            local cnt = tonumber(conf.mountCount)
            local speed = tonumber(conf.speedPlus)
            if hasNum < cnt then
                nextSpeed = speed
                needNum = cnt
                break
            elseif hasNum == cnt then
                curSpeed = speed
                curLv = lv
                if i == lenth then
                    nextSpeed = speed
                    needNum = cnt
                else
                    local nextConf = confs[i+1]
                    nextSpeed = tonumber(nextConf.speedPlus)
                    needNum = tonumber(nextConf.mountCount)
                end
                break
            else
                curLv = lv
                curSpeed = speed
            end
        end
        
        self._speedData = {
            curLv = curLv,
            curSpeed = curSpeed, 
            nextSpeed = nextSpeed, 
            hasNum = hasNum, 
            needNum = needNum
        }
    end
    return self._speedData
end

-- 获取所有激活坐骑属性
function ClassRef:getActiveData()
    local allAttrs = {}
    local skills = {}
    local mounts = UD.activatedMounts
    for _,mount in pairs(mounts) do
        local attrs = mount:getAttrs()
        for k,v in pairs(attrs) do
            if allAttrs[k] then
                allAttrs[k] = allAttrs[k] + v
            else
                allAttrs[k] = v
            end
        end
        skills[#skills + 1] = mount:getName() .. ": " .. mount:getSkillDesc()
    end

    return allAttrs, skills
end

----------------------------------------- request ------------------------------------
--装备坐骑
function ClassRef:reqEquipMount(uid)
    local spMount = UD:isSpRewardMount(uid)
    local type = 0
    if spMount then
        type = 1
    end

    app.session:request("action.C2S_EquipMount",{uid = uid, type = type},function(msg)
        -- 使用 UD.player 的更新消息
        -- app:sendMsg("MountController", "onUpdateEquipMount")
    end)
end

--卸载坐骑
function ClassRef:reqUnEquipMount()
    local mount = UD:getMount()
    if mount ~= nil then
        local spMount = UD:isSpRewardMount(mount.uid)
        local type = 0
        if spMount then
            type = 1
        end
        app.session:request("action.C2S_UnEquipMount",{uid = mount.uid, type = type},function(msg)
            -- 使用 UD.player 的更新消息
            -- app:sendMsg("MountController", "onUpdateEquipMount")
        end)
    end
end

--装备野性之力
function ClassRef:reqActivateMount(uid)
    app.session:request("action.C2S_ActivateMount", {uid = uid} ,function(msg)
        app:sendMsg("MountController", "onUpdateActiveMount")
    end)
end

--卸载野性之力
function ClassRef:reqDeactivateMount(uid)
    app.session:request("action.C2S_DeactivateMount",{uid = uid},function(msg)
        app:sendMsg("MountController", "onUpdateActiveMount")
    end)
end

--升级坐骑星级
function ClassRef:reqUpgradeMountStar(uid)
    app.session:request("action.C2S_UpgradeMountStar",{uid = uid},function(msg)
        app:popMessage("mountstarupsuccess", {pushToast=true})
        app:sendMsg("MountController", "onUpdateMountStar")
    end)
end

--升级坐骑品质
function ClassRef:reqUpgradeMountLevel(uid)
    app.session:request("action.C2S_UpgradeMountLevel",{uid = uid},function(msg)
        app:popMessage("mountlevelupsuccess", {pushToast=true})
        app:sendMsg("MountController", "onUpdateMountQuality")
    end)
end

--合成坐骑
function ClassRef:reqComposeMount(tid)
    app.session:request("action.C2S_ComposeMount",{mountid = tid},function(msg)
        display.pushToast(L("lua_code_text_442"))
        -- app:sendMsg("MountController", "onUpdateMountQuality")
    end)
end

--[[
-- 检查 坐骑是否可以
function ClassRef:checkMountCanSummon()
    if not self._noHaveMounts then
        self:initMountData()
    end
    
    for k, v in ipairs(self._noHaveMounts) do
        if v:checkQualityCostItemsEnough() then
            UD.badge:setNum(Constant.FUNC_TYPE_MOUNT, 1)
            return true
        end
    end
    UD.badge:setNum(Constant.FUNC_TYPE_MOUNT, 0)
    return false
end
]]--

return ClassRef